/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Binhluan;
import pojo.Danhsachsanpham;
import pojo.Loaisanpham;
import pojo.Nguoidung;
import pojo.Sanpham;
import until.HibernateUtil;

/**
 *
 * @author Alvis
 */
public class LoaiSanPhamDao {
    private Session session;

    public LoaiSanPhamDao() {
        session = HibernateUtil.getSessionFactory().openSession();

    }
     public void Insert(Loaisanpham value) {
        try {
            session.getTransaction().begin();
            String sql1 = "select nextval('loaisanpham_maloaisanpham_seq')";
            Statement st = session.connection().createStatement();
            ResultSet rq = (ResultSet) st.executeQuery(sql1);
            int ma = 0;
            while (rq.next()) {
                ma = rq.getInt("nextval");
            }
            value.setMaloaisanpham(ma);
            session.save(value);
            session.flush();
            session.getTransaction().commit();
        } catch (SQLException ex) {
            Logger.getLogger(NguoiDungDao.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
     public void Update(Loaisanpham value, int id) {
        session.getTransaction().begin();
        Loaisanpham valueTemp = (Loaisanpham) session.get(Loaisanpham.class, id);
        valueTemp.setTenloaisanpham(value.getTenloaisanpham());
        valueTemp.setMota(value.getMota());
        session.update(valueTemp);
        session.flush();
        session.getTransaction().commit();
    }

    public void Delete(int id) {
        session.getTransaction().begin();
        String sql = "from Sanpham where maloaisanpham=?";
        Query query = session.createQuery(sql);
        query.setInteger(0,id);
        List result = query.list();
        Sanpham[] listSanPham = new Sanpham[result.size()];
        result.toArray(listSanPham);
        for(int i=0;i<listSanPham.length;i++)
        {
            sql = "from Binhluan where masanpham=?";
            query = session.createQuery(sql);
            query.setInteger(0,listSanPham[i].getMasanpham());
            result = query.list();
            Binhluan[] listBinhLuan = new Binhluan[result.size()];
            result.toArray(listBinhLuan);
            for(int j=0;j<listBinhLuan.length;j++)
            {
                session.delete(session.get(Binhluan.class,listBinhLuan[j].getMabinhluan()));
            }
            sql = "from Danhsachsanpham where masanpham=?";
            query = session.createQuery(sql);
            query.setInteger(0,listSanPham[i].getMasanpham());
            result = query.list();
            Danhsachsanpham[] listDanhSachSanPham = new Danhsachsanpham[result.size()];
            result.toArray(listDanhSachSanPham);
            for(int j=0;j<listDanhSachSanPham.length;j++)
            {
                session.delete(session.get(Danhsachsanpham.class,listDanhSachSanPham[j].getId()));
            }
            session.delete(session.get(Sanpham.class,listSanPham[i].getMasanpham()));
            
        }
        
        session.delete(session.get(Loaisanpham.class, id));
        session.flush();
        session.getTransaction().commit();
    }
     public Loaisanpham[] getAllProductType() {
        session.getTransaction().begin();
        String sql = "from Loaisanpham";
        Query query = session.createQuery(sql);
        List result = query.list();
        Loaisanpham[] listvalue = new Loaisanpham[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
     public Loaisanpham[] SearchProductType(String name) {
        session.getTransaction().begin();
        String sql = "from Loaisanpham where tenloaisanpham like ?";
        Query query = session.createQuery(sql);
        query.setString(0,"%"+name+"%");
        List result = query.list();
        Loaisanpham[] listvalue = new Loaisanpham[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
    public Loaisanpham getProductType(int id) {

        session.getTransaction().begin();
        String sql = "from Loaisanpham where maloaisanpham=?";
        Query query = session.createQuery(sql);

        query.setInteger(0,id);
        Loaisanpham result = (Loaisanpham) query.uniqueResult();
        session.flush();
        session.getTransaction().commit();
        return result;

    }
    public Loaisanpham getProductTypeByName(String name) {

        session.getTransaction().begin();
        String sql = "from Loaisanpham where tenloaisanpham=?";
        Query query = session.createQuery(sql);

        query.setString(0,name);
        Loaisanpham result = (Loaisanpham) query.uniqueResult();
        session.flush();
        session.getTransaction().commit();
        return result;

    }
    
}
